#pragma once
#include<iostream>
#include<assert.h>
#include<stdbool.h>
using namespace std;
namespace zyb
{
	template <class T>
	class vector
	{
		typedef T* iterator;
		typedef const T* const_iterator;
	public:
		vector()
		{}
		vector(vector<T> &v)
		{
			reserve(v.capacity());
			for (size_t i = 0; i < v.size(); i++)
			{
				_start[i] = v._start[i];
			}
			_finish = _start + v.size();
		}
		size_t size() const
		{
			return _finish - _start;
		}
		size_t capacity() const
		{
			return _endofstrong - _start;
		}
		iterator begin()const
		{
			return _start;
		}
		iterator end()const
		{
			return _finish;
		}
		bool empty()
		{
			return _start == _finish;
		}
		T& front()const
		{
			return *_start;
		}
		T& back()const
		{
			return *(_finish-1);
		}
		T& operator[](int i)const
		{
			return _start[i];
		}
		void resize(int n, T val=T())
		{
			if (n < capacity())
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n);
				for (size_t i = size(); i < n; i++)
				{
					*_finish = val;
					++_finish;
				}
			}

		}
		void reserve(const size_t n)
		{
			if (n > capacity())
			{
				T* tmp = new T[n];
				if (_start)
				{
					for (size_t i = 0; i < size(); i++)
					{
						tmp[i] = *(_start + i);
					}
					delete[] _start;
				}
				size_t len = size();
				_start = tmp;
				_finish = tmp + len;
				_endofstrong = _start + n;
			}
		}
		void insert(iterator pos, const T& val)
		{
			size_t len = _finish - pos + 1;
			if (_finish == _endofstrong)
			{
				size_t NewCapacity = (capacity() == 0 ? 4 : capacity() * 2);
				reserve(NewCapacity);
			}
			size_t end = size() - 1;
			while (len--)
			{
				_start[end + 1] = _start[end];
				end--;
			}
			_finish++;
			*pos = val;
		}
		void earse(iterator pos)
		{
			assert(pos >= _start );
			assert(pos < _finish);
			size_t len = _finish - pos;
			size_t Front = pos - _start;
			while (len--)
			{
				_start[Front] = _start[Front+1];
				Front++;
			}
			_finish--;
		}
		void pop_back()
		{
			assert(_finish != _start);
			--_finish;
		}
		void push_back(const T& val)
		{
			if (_finish == _endofstrong)
			{
				size_t NewCapacity = (capacity() == 0 ? 4 : capacity() * 2);
				reserve(NewCapacity);
			}
			*(_finish) = val;
			_finish ++;
		}
	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _endofstrong = nullptr;
	};
}